package com.mego.msnavi.activitys;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.Toast;

import com.amap.api.maps.model.LatLng;
import com.amap.api.navi.AMapNaviView;
import com.amap.api.navi.enums.IconType;
import com.amap.api.navi.enums.NaviType;
import com.amap.api.navi.model.AMapNaviLocation;
import com.amap.api.navi.model.NaviInfo;
import com.amap.api.navi.model.NaviLatLng;
import com.mego.msnavi.R;
import com.mego.msnavi.util.IFlyTTS;
import com.mego.msnavi.util.NaviUtil;
import com.mego.msnavi.util.SystemCenter;

public class WalkRouteCalculateActivity extends BaseActivity {

    private IFlyTTS iflyTTS = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_walkroutecalculate);

        mAMapNaviView = (AMapNaviView) findViewById(R.id.navi_view);
        mAMapNaviView.onCreate(savedInstanceState);
        mAMapNaviView.setAMapNaviViewListener(this);
        mAMapNaviView.setNaviMode(AMapNaviView.NORTH_UP_MODE);

        mAMapNavi.addAMapNaviListener(this);
        mAMapNavi.setUseInnerVoice(false);//设置false后，回调 onGetNavigationText

        iflyTTS = IFlyTTS.getInstance(this);
        iflyTTS.init();
    }

    boolean isNaving = false;//正在导航

    //等待定位信号
    @Override
    public void onLocationChange(AMapNaviLocation location) {
        super.onLocationChange(location);
        //当前位置回调
        SystemCenter.getInstance().gpsLL = location.getCoord();
        if (!isNaving) {
            mHandler.sendEmptyMessage(DO_NAVI);
        }
    }

    private final int DO_NAVI = 66;
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case DO_NAVI:
                    doNavi();
                    break;
            }
        }
    };

    private void doNavi() {
        isNaving = true;
        LatLng llDest = SystemCenter.getInstance().poi.getCoordinate();//目的地
        mAMapNavi.calculateWalkRoute(SystemCenter.getInstance().gpsLL, new NaviLatLng(llDest.latitude, llDest.longitude));
    }

    @Override
    public void onInitNaviSuccess() {
        super.onInitNaviSuccess();
        Toast.makeText(this, "等待GPS信号!", Toast.LENGTH_SHORT);
    }

    @Override
    public void onCalculateRouteSuccess(int[] ids) {
        super.onCalculateRouteSuccess(ids);
        mAMapNavi.startNavi(NaviType.GPS);
    }

    long _voiceTime = 0;//上次播报时间，间隔1分钟播报

    @Override
    public void onNaviInfoUpdate(NaviInfo naviInfo) {
        super.onNaviInfoUpdate(naviInfo);
        //导航过程中的信息更新，请看NaviInfo的具体说明
        int allLength = mAMapNavi.getNaviPath().getAllLength();
        int turntype = naviInfo.getIconType();
        String nextroadname = naviInfo.getNextRoadName();
        String nextDistance = NaviUtil.formatKM(naviInfo.getCurStepRetainDistance());

        long thisTime = System.currentTimeMillis();
        long X = thisTime - _voiceTime;

        if (!iflyTTS.isPlaying() && !isFirst && X > 6000) {
            String strTurn = "去往";
            switch (turntype) {
                default:
                case IconType.STRAIGHT:
                    strTurn = "直行到";
                    break;
                case IconType.LEFT:
                    strTurn = "左转到";
                    break;
                case IconType.RIGHT:
                    strTurn = "右转到";
                    break;
                case IconType.LEFT_FRONT:
                    strTurn = "往左前方转到";
                    break;
                case IconType.RIGHT_FRONT:
                    strTurn = "往右前方转到";
                    break;
                case IconType.LEFT_BACK:
                    strTurn = "往左后方转到";
                    break;
                case IconType.RIGHT_BACK:
                    strTurn = "往右后方转到";
                    break;
                case IconType.LEFT_TURN_AROUND:
                    strTurn = "请左转掉头到";
                    break;
            }
            String info = "前方" + nextDistance + strTurn + nextroadname;
            Log.d("msnavi", "2   " + info);
            iflyTTS.playText(info);
            _voiceTime = thisTime;
        }
    }

    boolean isFirst = true;//首次播报

    @Override
    public void onGetNavigationText(int type, String text) {
        super.onGetNavigationText(type, text);
        //播报类型和播报文字回调
        String info = "1   " + text + " " + type;
        Log.d("msnavi", info);
        if (!iflyTTS.isPlaying()) {
            iflyTTS.playText(text);
            _voiceTime = System.currentTimeMillis();
            isFirst = false;
        }
    }

}